package controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import service.AdminTypeService;
import po.Goods;
import service.GoodsService;
import util.MyUtil;

@Controller
@RequestMapping("/goods")
/**
* @author    韩
* @version   创建时间：2020年12月6日 下午6:48:20
* @Class     18卓工
* @studentid 18130502
*/
public class GoodsController {
	@Autowired
	private GoodsService goodsService;
    @Autowired
    private AdminTypeService adminService;
    //前往添加页面
    @RequestMapping("/toAddGoods")
    public String toAddGoods(Model model){
    	model.addAttribute("goods", new Goods());
    	model.addAttribute("goodsType", adminService.getAllType());
    	
    	return "addGoods";
    }
    //添加和修改
    @RequestMapping("/addGoods")
	public String addGoods(@Valid @ModelAttribute Goods goods,BindingResult result,HttpServletRequest request,String updateAct,Model model){
		if(result.hasErrors()){
			if("update".equals(updateAct)){
				model.addAttribute("goodsType", adminService.getAllType());
				return "updateAgoods";

			}else{
				model.addAttribute("goodsType", adminService.getAllType());
				return "addGoods";
			}
	    	
		}
		
		//防止文件重名
		String newFileName="";
		//文件原始名
		String fileName=goods.getLogoImage().getOriginalFilename();
		//选择文件
		if(fileName.length()>0){
			
			String realpath=request.getServletContext().getRealPath("logos");
			//实现文件上传
			String fileType=fileName.substring(fileName.lastIndexOf('.'));
			newFileName=MyUtil.getStringID()+fileType;
			goods.setGpicture(newFileName);
			File targetFile=new File(realpath,newFileName);
			if(!targetFile.exists()){
				targetFile.mkdir();
			}
			try {
				goods.getLogoImage().transferTo(targetFile);
				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			
		}
		
		
		if("update".equals(updateAct)){
			    goodsService.updateGoods(goods);
				return "forward:/goods/selectGoods?act=updateSelect";
			
		}
		else{
			
			  goodsService.addGoods(goods);
				//转发到查询的controller
				
				return "forward:/goods/selectGoods";
		}
	}
    @RequestMapping("/toDeleteAGoods")
    public String toDeleteAGoods(Model model){
    	model.addAttribute("msg", "");
    	return "forward:/goods/selectGoods?act=deleteSelect";
    }
    //删除单个商品
  	@RequestMapping("/deleteAGoods")
  	public String deleteAGoods(Integer id,Model model){
  		if(goodsService.selectCartGoods(id).size()>0||goodsService.selectFocusGoods(id).size()>0||goodsService.selectOrderGoods(id).size()>0){
			model.addAttribute("msg", "商品有关联，不允许删除！！！");
			return "forward:/goods/selectGoods?act=deleteSelect";
		}
  		else{
		goodsService.deleteAGoods(id);
		model.addAttribute("msg", "删除成功！");
		return "forward:/goods/selectGoods?act=deleteSelect";
		}
  	}
    //删除多个商品
  	@RequestMapping("/deleteGoods")
  	public String deleteGoods(Model model,Integer ids[]){
  		List<Integer> list=new ArrayList<Integer>();
		for(int i=0;i<ids.length;i++){
			if(goodsService.selectCartGoods(ids[i]).size()>0||goodsService.selectFocusGoods(ids[i]).size()>0||goodsService.selectOrderGoods(ids[i]).size()>0){
				model.addAttribute("msg", "商品有关联，不允许删除！！！");
				return "forward:/goods/selectGoods?act=deleteSelect";
			}
			list.add(ids[i]);
		}
		goodsService.deleteGoods(list);
		model.addAttribute("msg", "删除成功！");
		return "forward:/goods/selectGoods?act=deleteSelect";
  	}
    //查询单个商品
  	@RequestMapping("/selectAGoods")
  	public String selectAGoods(Model model,Integer id,String act){
  		
  		Goods agoods=goodsService.selectAgoods(id);
		model.addAttribute("goods", agoods);
		if("updateAgoods".equals(act)){
			model.addAttribute("goodsType", adminService.getAllType());
			return "updateAgoods";
		}
		return "goodsDetail";
  	}
    //查询全部商品
  	@RequestMapping("/selectGoods")
  	public String selectGoods(Model model,Integer pageCur,String act){
  		List<Goods> allGoods=new ArrayList<Goods>();
		allGoods=goodsService.selectGoods();
		int temp=allGoods.size();
		model.addAttribute("totalCount", temp);
		int totalPage=0;
		if(temp==0){
			totalPage=0;
		}else{
			totalPage=(int)Math.ceil((double)temp/10);
		}
		if(pageCur==null){
			pageCur=1;
		}
		if((pageCur-1)*10>temp){
			pageCur=pageCur-1;
		}
		
		//分页查询
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("startIndex",(pageCur-1)*10);
		map.put("perPageSize", 10);
		allGoods=goodsService.selectGoodsByPage(map);
		model.addAttribute("allGoods", allGoods);
		model.addAttribute("totalPage", totalPage);
		model.addAttribute("pageCur", pageCur);
		
		//删除查询
		if("deleteSelect".equals(act)){
			return "deleteSelectGoods";
		}
		//修改查询
		else if("updateSelect".equals(act)){
			return "updateSelectGoods";
		}
		
		else{
			return "selectGoods";
		}
		
  	}
    
	
}

